<template>
  <view class="clan-wall">
    <!-- 空状态占位 -->
    <view v-if="!hasHonorData" class="empty-container">
      <view class="empty-icon-placeholder">
        <text class="empty-icon-text">🏆</text>
      </view>
      <view class="empty-text">暂无荣誉墙数据</view>
      <!-- <view class="empty-desc">快来添加第一个荣誉人物吧</view> -->
    </view>

    <!-- 荣誉墙数据展示 -->
    <view v-else class="wall-grid">
      <view v-for="(honor, index) in honorList" :key="honor.id || index" class="wall-item">
        <view class="award-name">{{ honor.name || "荣誉名称" }}</view>
        <view v-if="honor.desc" class="award-desc">{{ honor.desc }}</view>
        <view v-if="honor.createTime" class="award-time">{{ formatTime(honor.createTime) }}</view>
      </view>
      <!-- 添加荣誉按钮 -->
      <!-- <view class="wall-item add-item" @tap="showAddModal">
        <view class="add-icon">+</view>
        <view class="add-text">添加荣誉</view>
      </view> -->
    </view>

    <!-- 添加荣誉弹窗 -->
    <view v-if="showModal" class="modal-overlay" @tap="closeModal">
      <view class="modal-content" @tap.stop="">
        <view class="modal-header">
          <text class="modal-title">添加荣誉人物</text>
          <text class="modal-close" @tap="closeModal">×</text>
        </view>

        <view class="modal-body">
          <!-- <view class="form-item">
            <text class="form-label">年份</text>
            <input 
              v-model="formData.year"
              class="form-input" 
              type="number" 
              placeholder="请输入年份" 
              maxlength="4"
            />
          </view> -->

          <view class="form-item">
            <text class="form-label">姓名</text>
            <input
              v-model="formData.name"
              class="form-input"
              type="text"
              placeholder="请输入姓名"
              maxlength="20"
            />
          </view>

          <view class="form-item">
            <text class="form-label">荣誉描述</text>
            <textarea
              v-model="formData.desc"
              class="form-textarea"
              placeholder="请输入荣誉描述"
              maxlength="200"
              auto-height
            />
          </view>
        </view>

        <view class="modal-footer">
          <button class="btn-cancel" @tap="closeModal">取消</button>
          <button class="btn-submit" :disabled="!isFormValid" @tap="submitHonor">提交</button>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, onMounted, watch, computed } from "vue";
import HonorPersonAPI from "@/api/genealogy/honorPerson";

interface HonorPerson {
  id?: string;
  name: string;
  desc?: string;
  clanAssociationId?: string;
  createTime?: string;
}

interface Award {
  name: string;
}

interface Props {
  awardList: Award[];
  clanId?: string;
}

const props = withDefaults(defineProps<Props>(), {
  clanId: "",
});

// 数据相关
const honorList = ref<HonorPerson[]>([]);
const loading = ref(false);

// 弹窗相关
const showModal = ref(false);
const formData = ref<HonorPerson>({
  name: "",
  desc: "",
});

// 表单验证
const isFormValid = computed(() => {
  return formData.value.name;
});

// 计算属性：判断是否有荣誉数据
const hasHonorData = computed(() => {
  return honorList.value && honorList.value.length > 0;
});

// 获取荣誉人物列表
const getHonorPersonList = async () => {
  if (!props.clanId) {
    console.warn("clanId为空，无法获取荣誉人物数据");
    return;
  }

  loading.value = true;
  try {
    const res = await HonorPersonAPI.getHonorPersonPages({
      pageNum: 1,
      pageSize: 20,
      clanAssociationId: props.clanId,
    });

    if (res && res.list) {
      honorList.value = res.list.map((item: any) => ({
        id: item.id,
        name: item.name || "未知",
        desc: item.desc || "",
        createTime: item.createTime || "",
      }));
      console.log("荣誉人物列表:", honorList.value);
    }
  } catch (error) {
    console.error("获取荣誉人物列表失败:", error);
    uni.showToast({
      title: "获取数据失败",
      icon: "none",
    });
  } finally {
    loading.value = false;
  }
};

// 添加荣誉人物
const submitHonor = async () => {
  if (!isFormValid.value || !props.clanId) return;

  try {
    const submitData = {
      ...formData.value,
    };

    await HonorPersonAPI.addHonorPerson(submitData);

    uni.showToast({
      title: "添加成功",
      icon: "success",
    });

    // 重新获取列表
    await getHonorPersonList();

    // 关闭弹窗并重置表单
    closeModal();
  } catch (error) {
    console.error("添加荣誉人物失败:", error);
    uni.showToast({
      title: "添加失败，请重试",
      icon: "none",
    });
  }
};

// 表单输入处理 - UniApp v-model 语法糖
const onNameInput = (e: any) => {
  formData.value.name = e.detail.value;
};

const onDescInput = (e: any) => {
  formData.value.desc = e.detail.value;
};

// 弹窗控制
const showAddModal = () => {
  showModal.value = true;
  formData.value = {
    name: "",
    desc: "",
  };
};

const closeModal = () => {
  showModal.value = false;
  formData.value = {
    name: "",
    desc: "",
  };
};

// 格式化时间
const formatTime = (timeStr: string) => {
  if (!timeStr) return "";
  try {
    const date = new Date(timeStr);
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate()).padStart(2, "0");
    return `${year}-${month}-${day}`;
  } catch (error) {
    return timeStr;
  }
};

// 监听clanId变化
watch(
  () => props.clanId,
  (newClanId) => {
    if (newClanId) {
      getHonorPersonList();
    }
  },
  { immediate: true }
);

onMounted(() => {
  getHonorPersonList();
});
</script>

<style scoped>
.clan-wall {
  border-radius: 20rpx;
  padding: 32rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 8rpx 24rpx rgba(37, 104, 255, 0.1);
}

.wall-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20rpx;
}

.wall-item {
  border: 2rpx solid #f1f4ff;
  padding: 28rpx 20rpx;
  border-radius: 16rpx;
  text-align: center;
  transition: all 0.3s ease;
  box-shadow: 0 4rpx 12rpx rgba(37, 104, 255, 0.08);
  background: #fff;
}

.wall-item:hover {
  transform: translateY(-4rpx);
  box-shadow: 0 8rpx 20rpx rgba(37, 104, 255, 0.15);
}

/* 添加荣誉按钮样式 */
.add-item {
  border: 2rpx dashed #2568ff;
  background: rgba(37, 104, 255, 0.05);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 120rpx;
  cursor: pointer;
}

.add-item:hover {
  background: rgba(37, 104, 255, 0.1);
  border-color: #ff6b35;
}

.add-icon {
  font-size: 48rpx;
  color: #2568ff;
  font-weight: 300;
  margin-bottom: 8rpx;
}

.add-text {
  font-size: 24rpx;
  color: #2568ff;
  font-weight: 500;
}

/* 荣誉展示样式 */
.award-name {
  font-size: 26rpx;
  font-weight: 700;
  line-height: 1.4;
  color: #333;
  margin-bottom: 8rpx;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.award-desc {
  font-size: 22rpx;
  color: #666;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  margin-bottom: 8rpx;
}

.award-time {
  font-size: 20rpx;
  color: #999;
  line-height: 1.2;
  font-weight: 400;
  background: rgba(37, 104, 255, 0.08);
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
  display: inline-block;
  margin-top: 4rpx;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: #fff;
  border-radius: 20rpx;
  width: 85%;
  max-width: 600rpx;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.3);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 30rpx;
  border-bottom: 1rpx solid #eee;
}

.modal-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
}

.modal-close {
  font-size: 40rpx;
  color: #999;
  cursor: pointer;
  padding: 0 10rpx;
}

.modal-body {
  padding: 30rpx;
}

.form-item {
  margin-bottom: 30rpx;
}
.form-label {
  display: block;
  font-size: 28rpx;
  color: #333;
  margin-bottom: 12rpx;
  font-weight: 500;
}

.form-textarea {
  width: 100%;
  padding: 20rpx;
  border: 2rpx solid #e1e5e9;
  border-radius: 12rpx;
  font-size: 28rpx;
  box-sizing: border-box;
  background-color: #fafbfc;
  transition: all 0.3s ease;
}

.form-input:focus,
.form-textarea:focus {
  border-color: #2568ff;
  background-color: #fff;
  outline: none;
  box-shadow: 0 0 0 3rpx rgba(37, 104, 255, 0.1);
}

.form-input:hover,
.form-textarea:hover {
  border-color: #c0c4cc;
}

.form-textarea {
  min-height: 120rpx;
  resize: vertical;
}

.form-input:focus,
.form-textarea:focus {
  border-color: #2568ff;
  outline: none;
}
.form-input {
  border: 2rpx solid #e1e5e9;
  border-radius: 12rpx;
  padding: 20rpx;
}

.modal-footer {
  display: flex;
  justify-content: space-between;
  padding: 30rpx;
  border-top: 1rpx solid #eee;
}

.btn-cancel,
.btn-submit {
  flex: 1;
  padding: 20rpx 0;
  border: none;
  border-radius: 8rpx;
  font-size: 28rpx;
  cursor: pointer;
  margin: 0 10rpx;
}

.btn-cancel {
  background: #f5f5f5;
  color: #666;
}

.btn-submit {
  background: #2568ff;
  color: #fff;
}

.btn-submit:disabled {
  background: #ccc;
  cursor: not-allowed;
}

/* 空状态样式 */
.empty-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 120rpx 40rpx;
  text-align: center;
  min-height: 400rpx;
}

.empty-icon-placeholder {
  width: 160rpx;
  height: 160rpx;
  margin-bottom: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
  border-radius: 50%;
}

.empty-icon-text {
  font-size: 80rpx;
  opacity: 0.6;
}

.empty-text {
  font-size: 32rpx;
  font-weight: 600;
  color: #666;
  margin-bottom: 16rpx;
}

.empty-desc {
  font-size: 26rpx;
  color: #999;
  line-height: 1.5;
}

/* 响应式适配 */
@media (max-width: 768rpx) {
  .wall-grid {
    grid-template-columns: 1fr;
  }

  .modal-content {
    width: 95%;
    margin: 20rpx;
  }
}
</style>
